package com.ruoyi.cms.fileInfo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.cms.fileInfo.domain.CmsFileInfo;
import com.ruoyi.cms.fileInfo.mapper.CmsFileInfoMapper;
import com.ruoyi.cms.fileInfo.service.ICmsFileInfoService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import javax.annotation.Resource;

@Service
public class CmsFileInfoServiceImpl implements ICmsFileInfoService {


    @Resource
    private CmsFileInfoMapper cmsFileInfoMapper;


    @Override
    public CmsFileInfo selectCmsFileInfoByFileObjectName(String fileObjectName) {
        return cmsFileInfoMapper.selectCmsFileInfoByFileObjectName(fileObjectName);
    }

    /**
     * 查询文件管理
     *
     * @param fileId 文件管理主键
     * @return 文件管理
     */
    @Override
    public CmsFileInfo selectCmsFileInfoByFileId(Long fileId) {
        return cmsFileInfoMapper.selectCmsFileInfoByFileId(fileId);
    }

    /**
     * 查询文件管理列表
     *
     * @param cmsFileInfo 文件管理
     * @return 文件管理
     */
    @Override
    public List<CmsFileInfo> selectCmsFileInfoList(CmsFileInfo cmsFileInfo) {
        return cmsFileInfoMapper.selectCmsFileInfoList(cmsFileInfo);
    }

    /**
     * 新增文件管理
     *
     * @param cmsFileInfo 文件管理
     * @return 结果
     */
    @Override
    public int insertCmsFileInfo(CmsFileInfo cmsFileInfo) {
        cmsFileInfo.setCreateTime(DateUtils.getNowDate());
        return cmsFileInfoMapper.insertCmsFileInfo(cmsFileInfo);
    }

    /**
     * 修改文件管理
     *
     * @param cmsFileInfo 文件管理
     * @return 结果
     */
    @Override
    public int updateCmsFileInfo(CmsFileInfo cmsFileInfo) {
        cmsFileInfo.setUpdateTime(DateUtils.getNowDate());
        return cmsFileInfoMapper.updateCmsFileInfo(cmsFileInfo);
    }

    /**
     * 批量删除文件管理
     *
     * @param fileIds 需要删除的文件管理主键
     * @return 结果
     */
    @Override
    public int deleteCmsFileInfoByFileIds(Long[] fileIds) {
        for (int i = 0; i < fileIds.length; i++) {
            Long fileId = fileIds[i];
            CmsFileInfo cmsFileInfo = cmsFileInfoMapper.selectCmsFileInfoByFileId(fileId);
            String filePath = RuoYiConfig.getProfile() + StringUtils.substringAfter(cmsFileInfo.getFilePath(), Constants.RESOURCE_PREFIX);
            FileUtils.deleteFile(filePath);
        }
        return cmsFileInfoMapper.deleteCmsFileInfoByFileIds(fileIds);
    }

    /**
     * 删除文件管理信息
     *
     * @param fileId 文件管理主键
     * @return 结果
     */
    @Override
    public int deleteCmsFileInfoByFileId(Long fileId) {
        CmsFileInfo cmsFileInfo = cmsFileInfoMapper.selectCmsFileInfoByFileId(fileId);
        String filePath = RuoYiConfig.getProfile() + StringUtils.substringAfter(cmsFileInfo.getFilePath(), Constants.RESOURCE_PREFIX);
        FileUtils.deleteFile(filePath);
        return cmsFileInfoMapper.deleteCmsFileInfoByFileId(fileId);
    }

    @Override
    public int deleteCmsFileInfoByFileObjectName(String fileObjectName) {
        CmsFileInfo cmsFileInfo = cmsFileInfoMapper.selectCmsFileInfoByFileObjectName(fileObjectName);
        if (ObjectUtil.isEmpty(cmsFileInfo)) {
            //不存在当前文件信息，默认当前文件已经被删除
            return 0;
        }
        String filePath = RuoYiConfig.getProfile() + StringUtils.substringAfter(cmsFileInfo.getFilePath(), Constants.RESOURCE_PREFIX);
        FileUtils.deleteFile(filePath);
        return cmsFileInfoMapper.deleteCmsFileInfoByFileObjectName(fileObjectName);
    }
}
